package com.kedll.library.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ProgressBar;

import com.kedll.library.R;
import com.kedll.library.util.ToastUtil;
import com.kedll.library.util.ViewUtil;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.net.URLDecoder;

/**
 * 需要用H5加载页面的基类
 * 继承此类的子类需要实现loadURL方法和setTitle方法
 * 继承此类的资料，需Manifest中定义为process =“.web”,这样在onDestroy方法中，调用System.exit(0)
 * 用以解决webView 内存溢出的坑
 * Created by QiQi on 16/9/6.
 */
public abstract class KBaseWebViewActivity extends MyBaseFragmentActivity {
    protected WebView mWebView;
    ProgressBar progressBar;

    @Override
    protected int getLayout() {
        return R.layout.general_web;
    }

    @Override
    protected void initEvent() {
        mWebView = findViewById(R.id.webView);
        progressBar = findViewById(R.id.progressBar);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWebView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                ViewUtil.setGone(progressBar, false);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ViewUtil.setGone(progressBar, true);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressBar.setProgress(newProgress);
            }



            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                ToastUtil.showToast(URLDecoder.decode(message));
                result.confirm();
                return true;
            }

        });

        loadUrl();
    }

    protected abstract void loadUrl();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle(getToolBarTitle());

    }

    protected abstract String getToolBarTitle();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isFinishing()) {
            mWebView.destroy();
            mWebView.removeAllViews();
            mWebView = null;
        }
    }
}
